package com.gopay.common.cps.dao.bt.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.bt.CpsBtContractInfoQueryDAO;
import com.gopay.common.domain.cps.CpsBtContractInfo;
import com.gopay.common.domain.cps.vo.BtContractInfoBean;
import com.gopay.common.domain.pg.PgBtQueryTransDtl;
import com.gopay.common.util.DateUtils;
import oracle.jdbc.OracleTypes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * 大宗合约签约解约信息表
 * 
 * @CpsBtContractInfoQueryDAOImpl.java
 * @author xiefei
 * @2013-1-22 上午11:31:15 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("cpsBtContractInfoQueryDAO")
public class CpsBtContractInfoQueryDAOImpl extends BaseDAOImpl<CpsBtContractInfo, Serializable> implements
        CpsBtContractInfoQueryDAO {
    protected final static Log logger = LogFactory.getLog(CpsBtContractInfoQueryDAOImpl.class);

    @Override
    public List<BtContractInfoBean> queryContractInfo(PgBtQueryTransDtl d) {
        return searchCpsBlocktradeContractInfo(d);
    }

    public List<BtContractInfoBean> searchCpsBlocktradeContractInfo(final PgBtQueryTransDtl d) {
        //签约方类型
        String contracteeType = d.getMemberType();
        // 签约状态
        String state = d.getConstractType();
        String merOrderId = d.getMerOrderId();
        String merchantID = d.getMerId();
        String contracteeName = d.getMemberName();
        String signBeginTime = null == d.getTimeBegin2() ? "" : DateUtils.format(d.getTimeBegin2());
        String signEndTime = null == d.getTimeEnd2() ? "" : DateUtils.format(d.getTimeEnd2());
        String cancelBeginTime = null == d.getTimeBegin3() ? "" : DateUtils.format(d.getTimeBegin3());
        String cancelEndTime = null == d.getTimeEnd3() ? "" : DateUtils.format(d.getTimeEnd3());
        List<Object> inputList = new ArrayList<Object>();
        List<Integer> outList = new ArrayList<Integer>();
        inputList.add(contracteeType);
        inputList.add(state);
        inputList.add(merOrderId);
        inputList.add(merchantID);
        inputList.add(contracteeName);
        inputList.add(signBeginTime);
        inputList.add(signEndTime);
        inputList.add(cancelBeginTime);
        inputList.add(cancelEndTime);
        inputList.add(d.getResaultSetPageLines());
        inputList.add(d.getPageNum());
        inputList.add(1);
        outList.add(OracleTypes.CURSOR);
        int count = 0;
        Object rs;
        try {
            rs = this.executeProcedure("bt_contract_query", inputList, outList).get(0);
            count = null == rs ? 0:Integer.parseInt(String.valueOf(rs));
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Object> callForList = new ArrayList<Object>();
        callForList.add(contracteeType);
        callForList.add(state);
        callForList.add(merOrderId);
        callForList.add(merchantID);
        callForList.add(contracteeName);
        callForList.add(signBeginTime);
        callForList.add(signEndTime);
        callForList.add(cancelBeginTime);
        callForList.add(cancelEndTime);
        callForList.add(d.getResaultSetPageLines());
        callForList.add(d.getPageNum());
        callForList.add(0);
        List<Object> rss ;
        try {
            rss = this.executeProcedure("bt_contract_query", callForList, outList);
            return getCpsBlocktradeContractInfo(rss, count);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<BtContractInfoBean>();
        }
    }

    private List<BtContractInfoBean> getCpsBlocktradeContractInfo(List<Object> rs, int count) throws SQLException {
        List<BtContractInfoBean> ss = new ArrayList<BtContractInfoBean>();
        BtContractInfoBean s;
        if(rs.size() == 0){
            return ss;
        }else{
            for(int i=0;i<rs.size();i++){
                Object[] o = (Object[])rs.get(i);
                int c = 1;
                s = new BtContractInfoBean();
                s.setContractNo(String.valueOf(o[c++]));
                s.setContracterNo(String.valueOf(o[c++]));
                s.setContracterName(String.valueOf(o[c++]));
                s.setContracteeType(String.valueOf(o[c++]));
                s.setContracteeName(String.valueOf(o[c++]));
                s.setState(String.valueOf(o[c++]));
                s.setSignBeginTime(String.valueOf(o[c++]));
                s.setSignFinishTime(String.valueOf(o[c++]));
                s.setCancelBeginTime(String.valueOf(o[c++]));
                s.setCancelFinishTime(String.valueOf(o[c++]));
                s.setOrderId(String.valueOf(o[c++]));
                s.setDescription(String.valueOf(o[c++]));
                s.setSumCount(count);
                ss.add(s);
            }
        }
        logger.info("call bt_contract_query.END." + ss);
        return ss;
    }

    /**
     * 根据市场账户和会员账户查询合约信息
     * 
     * @param marketAcct
     * @param memberAcct
     * @return
     */
    @Override
    public List<CpsBtContractInfo> findByMarketAcctAndMemberAcct(String marketAcct, String memberAcct) {
        // 查询流水
        DetachedCriteria criteria = DetachedCriteria.forClass(CpsBtContractInfo.class);
        criteria.add(Restrictions.eq("marketAcct", marketAcct));
        criteria.add(Restrictions.eq("memberAcct", memberAcct));
        List<CpsBtContractInfo> orderList = this.findByCriteria(criteria);
        return orderList;
    }

    @Override
    public boolean isSign(String marketCustId, String memberAcct) {
        // 查询流水
        DetachedCriteria criteria = DetachedCriteria.forClass(CpsBtContractInfo.class);
        criteria.add(Restrictions.eq("marketCustId", marketCustId));
        criteria.add(Restrictions.eq("memberAcct", memberAcct));

        List<CpsBtContractInfo> orderList = this.findByCriteria(criteria);
        if(null!=orderList&&orderList.size()>0){
            for(CpsBtContractInfo info:orderList){
                if("1".equals(String.valueOf(info.getContractType()).trim())){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public CpsBtContractInfo getByContractNo(String contractNo) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CpsBtContractInfo.class);
        criteria.add(Restrictions.eq("contractId", contractNo));
        List<CpsBtContractInfo> list = this.findByCriteria(criteria);
        return list.size() > 0 ? list.get(0) : null;
    }
}
